package com.ning.activitys.skysound.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.ning.activitys.skysound.R;
import com.ning.activitys.skysound.base.BaseActivity;
import com.ning.activitys.skysound.base.MyApplication;
import com.ning.activitys.skysound.beans.GsonRadioDetails;
import com.ning.activitys.skysound.beans.MusicDetailsBeans;
import com.ning.activitys.skysound.beans.SongBeans;
import com.ning.activitys.skysound.datas.MusicDetailsTool;
import com.ning.activitys.skysound.service.PlayService;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by dllo on 16/1/13.
 */
public class PlayActivity extends BaseActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    private static final int UPDATE_TIME = 0x1;

    private SeekBar seekBarProgress;
    private TextView songName, startTime, endTime;
    private ImageView albumIage, songLast, songPlay, songNext, goBack, play_mode, likeSong;

    private static MyHandler myHandler;
    private SongBeans netSongBeans;

    MusicDetailsBeans musicDetailsBeans = new MusicDetailsBeans();

    //private ArrayList<MusicDetailsBeans> musicDetailsData;

    MyApplication myApplication = (MyApplication) getApplication();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);

        seekBarProgress = (SeekBar) findViewById(R.id.sb_progress_play);
        songName = (TextView) findViewById(R.id.tv_song_name_play);
        startTime = (TextView) findViewById(R.id.tv_song_start_time_play);
        endTime = (TextView) findViewById(R.id.tv_song_end_time_play);
        albumIage = (ImageView) findViewById(R.id.iv_album_image_play);
        songLast = (ImageView) findViewById(R.id.iv_song_last_play);
        songNext = (ImageView) findViewById(R.id.iv_song_next_play);
        songPlay = (ImageView) findViewById(R.id.iv_song_play_play);
        goBack = (ImageView) findViewById(R.id.iv_go_back_play);
        play_mode = (ImageView) findViewById(R.id.iv_play_mode_play);
        likeSong = (ImageView) findViewById(R.id.iv_like_song_play);

        albumIage.setOnClickListener(this);
        songLast.setOnClickListener(this);
        songPlay.setOnClickListener(this);
        songNext.setOnClickListener(this);
        goBack.setOnClickListener(this);
        play_mode.setOnClickListener(this);
        likeSong.setOnClickListener(this);
        seekBarProgress.setOnSeekBarChangeListener(this);

//        musicDetailsData = new ArrayList<>();
//        musicDetailsData = playService.getMusicDetailsData();


        //获取跳转是传的值
        //isPause = getIntent().getBooleanExtra("isPause", false);

        //先声明后赋值
        myHandler = new MyHandler(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        bindPlayService();
    }

    @Override
    protected void onPause() {
        super.onPause();
        unBindPlayService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unBindPlayService();

    }

    /**
     * SeekBar拖动监听
     **/
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser) {
            playService.pause();
            playService.seekTo(progress);
            playService.start();

            if (playService.isPlay()) {
                songPlay.setImageResource(R.mipmap.zantinganniusix);
            } else {
                songPlay.setImageResource(R.mipmap.bofanganniuone);
            }
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }


    /**
     * Handler更新时间
     */
    public static class MyHandler extends Handler {

        private PlayActivity playActivity;

        public MyHandler(PlayActivity playActivity) {

            this.playActivity = playActivity;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (playActivity != null) {
                switch (msg.what) {
                    case UPDATE_TIME:
                        playActivity.startTime.setText(MusicDetailsTool.formatTime(msg.arg1));
                        break;
                }
            }
        }
    }

    @Override
    public void publish(int progress) {
        //子线程
        // startTime.setText(progress+"");
//        Message msg = myHandler.obtainMessage(UPDATE_TIME);
//        msg.arg1 = progress;
//        myHandler.sendMessage(msg);
        Message message = new Message();
        message.what = UPDATE_TIME;
        message.arg1 = progress;
        myHandler.sendMessage(message);

        seekBarProgress.setProgress(progress);
    }

    @Override
    public void change(int position) {

        if (PlayService.IS_NET_MUSIC == 1) {

            MusicDetailsBeans musicDetailsBeans = playService.getMusicDetailsData().get(position);
            songName.setText(musicDetailsBeans.getTitle());
            endTime.setText(MusicDetailsTool.formatTime(musicDetailsBeans.getDuration()) + "");
            seekBarProgress.setProgress(0);
            seekBarProgress.setMax((int) musicDetailsBeans.getDuration());

            Bitmap albumBitmap = MusicDetailsTool.getArtwork(this,
                    musicDetailsBeans.getId(), musicDetailsBeans.getAlbumID(),
                    true, false);
            albumIage.setImageBitmap(albumBitmap);


            try {

                MusicDetailsBeans likeMusic = myApplication.dbUtils.findFirst(Selector.from(MusicDetailsBeans.class)
                        .where("myID", "=", myGetID(musicDetailsBeans)));

                if (likeMusic != null) {

                    if (likeMusic.getIsLike() == 1) {
                        Log.e("getIsLike", likeMusic.getIsLike() + "");

                        likeSong.setImageResource(R.mipmap.xin_hong);

                    } else if (likeMusic.getIsLike() == 0) {

                        likeSong.setImageResource(R.mipmap.xin_bai);
                    }
                } else {

                    likeSong.setImageResource(R.mipmap.xin_bai);
                }


            } catch (DbException e) {
                e.printStackTrace();
            }


        }

        if (PlayService.IS_NET_MUSIC == 0) {

            netSongBeans = playService.getNetSongBeans();
            if (null != netSongBeans) {

                songName.setText(netSongBeans.getTitle());
                endTime.setText(MusicDetailsTool.formatTime(netSongBeans.getDuration()));
                seekBarProgress.setMax((int)netSongBeans.getDuration());
            }

            seekBarProgress.setProgress(0);
        }

        if (playService.isPlay()) {
            songPlay.setImageResource(R.mipmap.zantinganniusix);
        } else {
            songPlay.setImageResource(R.mipmap.bofanganniuone);
        }

        switch (playService.getPlay_mode()) {
            case 1:
                play_mode.setImageResource(R.mipmap.order);
                break;

            case 2:
                play_mode.setImageResource(R.mipmap.random);
                break;

            case 3:
                play_mode.setImageResource(R.mipmap.single);
                break;
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_go_back_play:

                finish();
                break;

            case R.id.iv_song_play_play:
                //子类可以直接使用父类的属性
                if (playService.isPlay()) {
                    playService.pause();
                    songPlay.setImageResource(R.mipmap.bofanganniuone);

                } else {
                    if (playService.isPause()) {
                        playService.start();
                        songPlay.setImageResource(R.mipmap.zantinganniusix);
                    } else {
                        playService.play(playService.getCurrentPlayPosition());
                        songPlay.setImageResource(R.mipmap.zantinganniusix);
                    }
                }
                break;

            case R.id.iv_song_next_play:
                playService.next();
                break;

            case R.id.iv_song_last_play:
                playService.last();

                break;

            case R.id.iv_play_mode_play:
                switch (playService.getPlay_mode()) {
                    case 1:
                        play_mode.setImageResource(R.mipmap.random);
                        playService.setPlay_mode(2);
                        break;

                    case 2:
                        play_mode.setImageResource(R.mipmap.single);
                        playService.setPlay_mode(3);
                        break;

                    case 3:
                        play_mode.setImageResource(R.mipmap.order);
                        playService.setPlay_mode(1);
                        break;
                }
                break;

            /**
             * 点击收藏
             *
             * ID 音乐的ID
             * myID 新建的字段用来存储音乐ID
             * */
            case R.id.iv_like_song_play:
                musicDetailsBeans =
                        playService.getMusicDetailsData().get(playService.getCurrentPlayPosition());
                try {

                    MusicDetailsBeans likeMusic = myApplication.dbUtils.findFirst(Selector.from(MusicDetailsBeans.class)
                            .where("myID", "=", myGetID(musicDetailsBeans)));

                    if (likeMusic == null) {

                        musicDetailsBeans.setMyID(musicDetailsBeans.getId());
                        musicDetailsBeans.setIsLike(1);

                        myApplication.dbUtils.save(musicDetailsBeans);

                        likeSong.setImageResource(R.mipmap.xin_hong);
                    } else {
                        //myApplication.dbUtils.delete(musicDetailsBeans);
                        int isLike = likeMusic.getIsLike();

                        if (isLike == 1) {
                            likeMusic.setIsLike(0);

                            likeSong.setImageResource(R.mipmap.xin_bai);

                        } else {

                            likeMusic.setIsLike(1);
                            //myApplication.dbUtils.deleteById(MusicDetailsBeans.class, likeMusic.getId());
                            likeSong.setImageResource(R.mipmap.xin_hong);
                        }

                        myApplication.dbUtils.update(likeMusic, "isLike");
                    }

                } catch (DbException e) {
                    e.printStackTrace();
                }

                break;
        }
    }

    private long myGetID(MusicDetailsBeans musicDetailsBeans) {
        long id = 0;

        switch (playService.getPlatList()) {
            case PlayService.ALL_MUSIC:

                id = musicDetailsBeans.getId();
                break;

            case PlayService.LIKE_MUSIC:

                id = musicDetailsBeans.getMyID();
                break;

            default:
                break;

        }
        return id;
    }
    /**
     *  ALL_MUSIC 两个ID 都是音乐的ID 3358 3358
     *  LIKE_MUSIC 第一个是自己建的表的 id ,第二个是 音乐的ID  1  3358
     * */



}
